<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Sale extends MX_Controller {

	public function __construct() {
		parent::__construct();

		$this->template->set_layout('full_layout');
		$this->template->set_partial('header_search', 'header/header_search');
		$this->template->set_partial('nav_bar', 'header/nav_bar');
		$this->load->model('member/member_model');
		$this->load->model('member/profile_model');
		$this->load->model('member/sale_model');
		$this->load->model('location/location_model');
		$this->load->library('parser');
	}

	public function index(){
		$data['mode'] = "SALE";

		
		
		$isProfile = $this->profile_model->getProfileByMemberID($this->session->userdata('mem_id'));
		if($isProfile){
			$data['profile']['info'] = $isProfile;
		}else{
			$data['profile']['info'] = NULL;
		}
		
		$data['state']['all'] = $this->location_model->getStates();


		$this->template->build('sale',$data);
	}

	function add()
	{
		$this->form_validation->set_rules('sale_title', 'Sale Title', 'trim|required|xss_clean');
		$this->form_validation->set_rules('sale_address', 'Address', 'trim|required|xss_clean');
		$this->form_validation->set_rules('state', 'Province / State', 'trim|required|xss_clean');
		$this->form_validation->set_rules('city', 'City', 'trim|required|xss_clean');
		$this->form_validation->set_rules('contact_person', 'Contact Person', 'trim|required|xss_clean');
		$this->form_validation->set_rules('contact_no', 'Contact No', 'trim|required|xss_clean');
		$this->form_validation->set_rules('sale_price', 'Sale Price', 'trim|xss_clean');
		$this->form_validation->set_rules('price_type', 'Check price Type', 'trim|xss_clean');
		$this->form_validation->set_rules('sale_description', 'Description', 'trim|required|xss_clean');

		
		$this->form_validation->set_rules('sale_price', 'Sale Price', 'trim|xss_clean');

		$id = $this->input->post('sale_id');
			
		if ($this->form_validation->run() == true) {
			$post_data = array(
					'mem_id' =>  $this->session->userdata('mem_id'),
					'title' => $this->input->post('sale_title'),
					'address' => $this->input->post('sale_address'),
					'state' => $this->input->post('state'),
					'city' => $this->input->post('city'),
					'contact_person' => $this->input->post('contact_person'),
					'contact_no' => $this->input->post('contact_no'),
					'price_type' => $this->input->post('price_type'),
					'description' => $this->input->post('sale_description'),
					'price' => $this->input->post('sale_price'),
			
					'lat' => 	$this->input->post('lat'),
					'long' => $this->input->post('long'),
					);
			
			
			$this->db->insert('sale',$post_data);
		
			$sID = $this->_getLastID();

		
		$config['upload_path'] = FCPATH.'assets/images/sale/';
		$path=$config['upload_path'];
		$config['allowed_types'] = 'gif|jpg|jpeg|png';
		$config['max_size'] = '1024';
		$config['max_width'] = '1920';
		$config['max_height'] = '1280';
		$this->load->library('upload');
		
		$filear = array();
		$ctr=1;
		
		foreach ($_FILES as $key => $value) {

			if (!empty($value['tmp_name'])) {
				
				$fn = $value['name'];
				$ext = preg_replace("/.*\.([^.]+)$/", "\\1", strtolower($fn));
				$imagename=strtolower($fn);
				$imagename=md5(time().$value['tmp_name']);
				
				$config['file_name']     = $imagename;
				$this->upload->initialize($config);
				
				

				//print count($key);die();
				
				
				$upData = $this->upload->do_upload($key);
				if ( ! $upData) {
					$error = array('error' => $this->upload->display_errors());
					//failed display the errors
					print_r ($error);


				} else {
					$f = $imagename.".".$ext;
					array_push($filear,$f);
					
					//print $ctr; die();
					
					switch($ctr){
						case 1:
							$sql = "update sale set image1 = '$filear[0]' where id=$sID";
							$this->db->query($sql);
							break;
						case 2:
							$sql = "update sale set image2 = '$filear[1]' where id=$sID";
							$this->db->query($sql);
							break;
						case 3:
							$sql = "update sale set image3 = '$filear[2]' where id=$sID";
							$this->db->query($sql);
							break;
					
						case 4:
							$sql = "update sale set image4 = '$filear[3]' where id=$sID";
							$this->db->query($sql);
							break;
						case 5:
							$sql = "update sale set image5 = '$filear[4]' where id=$sID";
							$this->db->query($sql);
							break;
						case 6:
							$sql = "update sale set image6 = '$filear[5]' where id=$sID";
							$this->db->query($sql);
							break;
					}
						
					
				}

			}
			
			
			$ctr++;
		}
		
		$this->session->set_flashdata('message', 'Added Successfully');
		redirect('member/sale/index');
		}else{
		$this->index();
		}
	}



	function update()
	{
		$this->form_validation->set_rules('sale_title', 'Sale Title', 'trim|required|xss_clean');
		$this->form_validation->set_rules('sale_address', 'Address', 'trim|required|xss_clean');
		$this->form_validation->set_rules('state', 'Province / State', 'trim|required|xss_clean');
		$this->form_validation->set_rules('city', 'City', 'trim|required|xss_clean');
		$this->form_validation->set_rules('contact_person', 'Contact Person', 'trim|required|xss_clean');
		$this->form_validation->set_rules('contact_no', 'Contact No', 'trim|required|xss_clean');
		$this->form_validation->set_rules('sale_price', 'Sale Price', 'trim|xss_clean');
		$this->form_validation->set_rules('price_type', 'Check price Type', 'trim|xss_clean');
		$this->form_validation->set_rules('sale_description', 'Description', 'trim|required|xss_clean');
		
		
		$this->form_validation->set_rules('sale_price', 'Sale Price', 'trim|xss_clean');
		
		$id = $this->input->post('sale_id');
			
		if ($this->form_validation->run() == true) {
			$post_data = array(
					'mem_id' =>  $this->session->userdata('mem_id'),
					'title' => $this->input->post('sale_title'),
					'address' => $this->input->post('sale_address'),
					'state' => $this->input->post('state'),
					'city' => $this->input->post('city'),
					'contact_person' => $this->input->post('contact_person'),
					'contact_no' => $this->input->post('contact_no'),
					'price_type' => $this->input->post('price_type'),
					'description' => $this->input->post('sale_description'),
					'price' => $this->input->post('sale_price'),
					'lat' => 	$this->input->post('lat'),
					'long' => $this->input->post('long'),
			);
				
			$this->db->where('id',$id);	
			$this->db->update('sale',$post_data);
		
			$this->session->set_flashdata('message', 'Updated Successfully');
			redirect('member/sale/edit/'.$id);
		}else{
			$this->index();
		}
	}



	
	function image1() {

		$id = $this->input->post('sale_id');
		
		$filevariable=array_keys($_FILES);
		$userfile=$filevariable[0];
		$image=$_FILES[$userfile];
		
		$imageData=array(
					'userfile'=>$userfile,
					'imagearray'=>$image,
					'imagepath'=>'./assets/images/sale/',
					'allowedtype'=>'jpg|png|gif|jpeg',
					'overwrite'=>'true',
					'removespace'=>'true',
					'customefilename'=>'',     		//do not use extension if not need leave blank
					'namewithtime'=>'yes', 			//time(image_121326.jpg)at last of file name if not need leave blank
					'separator'=>'-', 				//format - image_121326.jpg (.)not allowed, if not require leave blank
					'errmessage'=>'',               // If blank default message will shown
					'sucessmessage'=>'',            // If blank default message will shown
					'createthumb'=>'no',
					'thumbpath'=>'' 				// Dont write thumb folder name in path

			);

			$uploadData=$this->sale_model->upload($imageData);
			$data=array(
					//'id'=>$listing_id,
					'image1'=>$uploadData['data'],

			);

			if ($uploadData['data']  == TRUE ) {


				$image = $this->sale_model->businessImage($id);

				if ($image->image1 != 'no-image.jpg') {
					$f = BASEPATH.'../assets/images/sale/' . $image->image1;
					unlink($f);
				}
				$this->db->where('id', $id);
				$this->db->update('sale',$data);
				$this->session->set_flashdata('msz', 'Image Uploded Sucessfully!... ');
				redirect('member/sale/edit/'.$id);
					
			} else {
					
				$this->session->set_flashdata('message', 'Fields can not be null!... ');
				redirect('member/sale/edit/'.$id);
				exit();

			}
		
	}
	
	function image2() {
	
		$id = $this->input->post('sale_id');
		$filevariable=array_keys($_FILES);
		$userfile=$filevariable[0];
		$image=$_FILES[$userfile];
	
		$imageData=array(
				'userfile'=>$userfile,
				'imagearray'=>$image,
				'imagepath'=>'./assets/images/sale/',
				'allowedtype'=>'jpg|png|gif|jpeg',
				'overwrite'=>'true',
				'removespace'=>'true',
				'customefilename'=>'',     		//do not use extension if not need leave blank
				'namewithtime'=>'yes', 			//time(image_121326.jpg)at last of file name if not need leave blank
				'separator'=>'-', 				//format - image_121326.jpg (.)not allowed, if not require leave blank
				'errmessage'=>'',               // If blank default message will shown
				'sucessmessage'=>'',            // If blank default message will shown
				'createthumb'=>'no',
				'thumbpath'=>'' 				// Dont write thumb folder name in path
	
		);
	
		$uploadData=$this->sale_model->upload($imageData);

		$data=array(
				//'id'=>$listing_id,
				'image2'=>$uploadData['data'],
	
		);
	
		if ($uploadData['data']  == TRUE ) {
	
	
			$image = $this->sale_model->businessImage($id);
	
			if ($image->image2 != 'no-image.jpg') {
				$f = BASEPATH.'../assets/images/sale/' . $image->image2;
				unlink($f);
			}
			$this->db->where('id', $id);
			$this->db->update('sale',$data);
			$this->session->set_flashdata('msz', 'Image Uploded Sucessfully!... ');
			redirect('member/sale/edit/'.$id);
				
		} else {
				
			$this->session->set_flashdata('msz', 'Fields can not be null!... ');
			redirect('member/sale/edit/'.$id);
			exit();
	
		}
	
	}
	
	function image3() {
	
		$id = $this->input->post('sale_id');
		$filevariable=array_keys($_FILES);
		$userfile=$filevariable[0];
		$image=$_FILES[$userfile];
	
		$imageData=array(
				'userfile'=>$userfile,
				'imagearray'=>$image,
				'imagepath'=>'./assets/images/sale/',
				'allowedtype'=>'jpg|png|gif|jpeg',
				'overwrite'=>'true',
				'removespace'=>'true',
				'customefilename'=>'',     		//do not use extension if not need leave blank
				'namewithtime'=>'yes', 			//time(image_121326.jpg)at last of file name if not need leave blank
				'separator'=>'-', 				//format - image_121326.jpg (.)not allowed, if not require leave blank
				'errmessage'=>'',               // If blank default message will shown
				'sucessmessage'=>'',            // If blank default message will shown
				'createthumb'=>'no',
				'thumbpath'=>'' 				// Dont write thumb folder name in path
	
		);
	
		$uploadData=$this->sale_model->upload($imageData);
		$data=array(
				//'id'=>$listing_id,
				'image3'=>$uploadData['data'],
	
		);
	
		if ($uploadData['data']  == TRUE ) {
	
	
			$image = $this->sale_model->businessImage($id);
	
			if ($image->image3 != 'no-image.jpg') {
				$f = BASEPATH.'../assets/images/sale/' . $image->image3;
				unlink($f);
			}
			$this->db->where('id', $id);
			$this->db->update('sale',$data);
			$this->session->set_flashdata('msz', 'Image Uploded Sucessfully!... ');
			redirect('member/sale/edit/'.$id);
	
		} else {
	
			$this->session->set_flashdata('msz', 'Fields can not be null!... ');
			redirect('member/sale/edit/'.$id);
			exit();
	
		}
	
	}
	
	function image4() {
	
		$id = $this->input->post('sale_id');
		$filevariable=array_keys($_FILES);
		$userfile=$filevariable[0];
		$image=$_FILES[$userfile];
	
		$imageData=array(
				'userfile'=>$userfile,
				'imagearray'=>$image,
				'imagepath'=>'./assets/images/sale/',
				'allowedtype'=>'jpg|png|gif|jpeg',
				'overwrite'=>'true',
				'removespace'=>'true',
				'customefilename'=>'',     		//do not use extension if not need leave blank
				'namewithtime'=>'yes', 			//time(image_121326.jpg)at last of file name if not need leave blank
				'separator'=>'-', 				//format - image_121326.jpg (.)not allowed, if not require leave blank
				'errmessage'=>'',               // If blank default message will shown
				'sucessmessage'=>'',            // If blank default message will shown
				'createthumb'=>'no',
				'thumbpath'=>'' 				// Dont write thumb folder name in path
	
		);
	
		$uploadData=$this->sale_model->upload($imageData);
		$data=array(
				//'id'=>$listing_id,
				'image4'=>$uploadData['data'],
	
		);
	
		if ($uploadData['data']  == TRUE ) {
	
	
			$image = $this->sale_model->businessImage($id);
	
			if ($image->image4 != 'no-image.jpg') {
				$f = BASEPATH.'../assets/images/sale/' . $image->image4;
				unlink($f);
			}
			$this->db->where('id', $id);
			$this->db->update('sale',$data);
			$this->session->set_flashdata('msz', 'Image Uploded Sucessfully!... ');
			redirect('member/sale/edit/'.$id);
	
		} else {
	
			$this->session->set_flashdata('msz', 'Fields can not be null!... ');
			redirect('member/sale/edit/'.$id);
			exit();
	
		}
	
	}
	function image5() {
	
		$id = $this->input->post('sale_id');
		$filevariable=array_keys($_FILES);
		$userfile=$filevariable[0];
		$image=$_FILES[$userfile];
	
		$imageData=array(
				'userfile'=>$userfile,
				'imagearray'=>$image,
				'imagepath'=>'./assets/images/sale/',
				'allowedtype'=>'jpg|png|gif|jpeg',
				'overwrite'=>'true',
				'removespace'=>'true',
				'customefilename'=>'',     		//do not use extension if not need leave blank
				'namewithtime'=>'yes', 			//time(image_121326.jpg)at last of file name if not need leave blank
				'separator'=>'-', 				//format - image_121326.jpg (.)not allowed, if not require leave blank
				'errmessage'=>'',               // If blank default message will shown
				'sucessmessage'=>'',            // If blank default message will shown
				'createthumb'=>'no',
				'thumbpath'=>'' 				// Dont write thumb folder name in path
	
		);
	
		$uploadData=$this->sale_model->upload($imageData);
		$data=array(
				//'id'=>$listing_id,
				'image5'=>$uploadData['data'],
	
		);
	
		if ($uploadData['data']  == TRUE ) {
	
	
			$image = $this->sale_model->businessImage($id);
	
			if ($image->image5 != 'no-image.jpg') {
				$f = BASEPATH.'../assets/images/sale/' . $image->image5;
				unlink($f);
			}
			$this->db->where('id', $id);
			$this->db->update('sale',$data);
			$this->session->set_flashdata('msz', 'Image Uploded Sucessfully!... ');
			redirect('member/sale/edit/'.$id);
	
		} else {
	
			$this->session->set_flashdata('msz', 'Fields can not be null!... ');
			redirect('member/sale/edit/'.$id);
			exit();
	
		}
	
	}
	function image6() {
	
		$id = $this->input->post('sale_id');
		$filevariable=array_keys($_FILES);
		$userfile=$filevariable[0];
		$image=$_FILES[$userfile];
	
		$imageData=array(
				'userfile'=>$userfile,
				'imagearray'=>$image,
				'imagepath'=>'./assets/images/sale/',
				'allowedtype'=>'jpg|png|gif|jpeg',
				'overwrite'=>'true',
				'removespace'=>'true',
				'customefilename'=>'',     		//do not use extension if not need leave blank
				'namewithtime'=>'yes', 			//time(image_121326.jpg)at last of file name if not need leave blank
				'separator'=>'-', 				//format - image_121326.jpg (.)not allowed, if not require leave blank
				'errmessage'=>'',               // If blank default message will shown
				'sucessmessage'=>'',            // If blank default message will shown
				'createthumb'=>'no',
				'thumbpath'=>'' 				// Dont write thumb folder name in path
	
		);
	
		$uploadData=$this->sale_model->upload($imageData);
		$data=array(
				//'id'=>$listing_id,
				'image6'=>$uploadData['data'],
	
		);
	
		if ($uploadData['data']  == TRUE ) {
	
	
			$image = $this->sale_model->businessImage($id);
	
			if ($image->image6 != 'no-image.jpg') {
				$f = BASEPATH.'../assets/images/sale/' . $image->image6;
				unlink($f);
			}
			$this->db->where('id', $id);
			$this->db->update('sale',$data);
			$this->session->set_flashdata('msz', 'Image Uploded Sucessfully!... ');
			redirect('member/sale/edit/'.$id);
	
		} else {
	
			$this->session->set_flashdata('msz', 'Fields can not be null!... ');
			redirect('member/sale/edit/'.$id);
			exit();
	
		}
	
	}
	
	function edit($id)
	{
		$data['mode'] = "EDIT";
		$isProfile = $this->profile_model->getProfileByMemberID($this->session->userdata('mem_id'));
		if($isProfile){
			$data['profile']['info'] = $isProfile;
		}else{
			$data['profile']['info'] = NULL;
		}
		$data['sale']['info'] = $this->sale_model->getsaleInfo($id);
		$data['state']['all'] = $this->location_model->getStates();
		$data['city']= $this->location_model->get_city($data['sale']['info']->state);
		$this->template->build('sale',$data);

	}
	function delete1($id,$image_file){
	
		$this->sale_model->delete($id,$image_file,'image1');
		redirect('member/sale/edit/'.$id);
	
	}
	
	function delete2($id,$image_file){
	
		$this->sale_model->delete($id,$image_file,'image2');
		redirect('member/sale/edit/'.$id);
	
	}
	
	function delete3($id,$image_file){
	
		$this->sale_model->delete($id,$image_file,'image3');
		redirect('member/sale/edit/'.$id);
	
	}
	function delete4($id,$image_file){
	
		$this->sale_model->delete($id,$image_file,'image4');
		redirect('member/sale/edit/'.$id);
	
	}
	function delete5($id,$image_file){
	
		$this->sale_model->delete($id,$image_file,'image5');
		redirect('member/sale/edit/'.$id);
	
	}
	function delete6($id,$image_file){
	
		$this->sale_model->delete($id,$image_file,'image6');
		redirect('member/sale/edit/'.$id);
	
	}
	
	
	function delete($id)
	{
		$this->sale_model->delete_sale($id);
		redirect('member');
	}
	
	function _getLastID(){
		$mem_id = $this->session->userdata('mem_id');
		$sql = "Select max(id) as id from sale where mem_id = $mem_id ";
		$result= $this->db->query($sql)->row();
		if ($result)
		{
			return $result->id;
		}
	}
	
	
	
	function detail()
	{
		$data['mode'] = "DETAIL";
		
		$id = $this->commoncall->get_id(current_url());
		
		$data['business']['info'] = $this->sale_model->getsaleInfo($id);
		
		
		
		$this->template->set_layout('sale');
		$this->template->build('member/sale',$data);
	}
	
	
}